package com.boot.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boot.entity.Article;
import com.boot.entity.Banner;
import com.boot.entity.Bbs;
import com.boot.entity.Cate;
import com.boot.entity.Clazz;
import com.boot.entity.Complains;
import com.boot.entity.Ctime;
import com.boot.entity.Orders;
import com.boot.entity.Rebbs;
import com.boot.entity.Rooms;
import com.boot.entity.Users;
import com.boot.service.ArticleService;
import com.boot.service.BannerService;
import com.boot.service.BbsService;
import com.boot.service.CateService;
import com.boot.service.ClazzService;
import com.boot.service.ComplainsService;
import com.boot.service.CtimeService;
import com.boot.service.OrdersService;
import com.boot.service.RebbsService;
import com.boot.service.RoomsService;
import com.boot.service.UsersService;
import com.boot.util.PageHelper;
import com.boot.util.VeDate;

//定义为控制器
@Controller
// 设置路径
@RequestMapping("/index")
public class IndexController extends BaseController {

	@Autowired
	private UsersService usersService;
	@Autowired
	private BannerService bannerService;
	@Autowired
	private ArticleService articleService;
	@Autowired
	private CateService cateService;
	@Autowired
	private RoomsService roomsService;
	@Autowired
	private ClazzService clazzService;
	@Autowired
	private CtimeService ctimeService;
	@Autowired
	private OrdersService ordersService;
	@Autowired
	private ComplainsService complainsService;
	@Autowired
	private BbsService bbsService;
	@Autowired
	private RebbsService rebbsService;

	// 公共方法 提供公共查询数据
	private void front() {
		this.getRequest().setAttribute("title", "教室和教师预约管理系统");
		List<Banner> bannerList = this.bannerService.getAllBanner();
		this.getRequest().setAttribute("bannerList", bannerList);
		List<Cate> cateList = this.cateService.getCateFront();
		this.getRequest().setAttribute("cateList", cateList);
		List<Rooms> hotList = this.roomsService.getRoomsByHot();
		this.getRequest().setAttribute("hotList", hotList);
	}

	// 首页显示
	@RequestMapping("index.action")
	public String index() {
		this.front();
		List<Cate> cateList = this.cateService.getCateFront();
		List<Cate> frontList = new ArrayList<Cate>();
		for (Cate cate : cateList) {
			List<Rooms> roomsList = this.roomsService.getRoomsByCate(cate.getCateid());
			cate.setRoomsList(roomsList);
			frontList.add(cate);
		}
		this.getRequest().setAttribute("frontList", frontList);
		return "users/index";
	}

	// 新闻公告
	@RequestMapping("article.action")
	public String article(String id, String number) {
		this.front();
		Article article = new Article();
		article.setBannerid(id);
		List<Article> articleList = this.articleService.getArticleByCond(article);
		PageHelper.getIndexPage(articleList, "article", "article", id, 10, number, this.getRequest());
		Banner banner = this.bannerService.getBannerById(id);
		this.getRequest().setAttribute("banner", banner);
		return "users/article";
	}

	// 阅读公告
	@RequestMapping("read.action")
	public String read(String id) {
		this.front();
		Article article = this.articleService.getArticleById(id);
		article.setHits("" + (Integer.parseInt(article.getHits()) + 1));
		this.articleService.updateArticle(article);
		this.getRequest().setAttribute("article", article);
		return "users/read";
	}

	// 准备登录
	@RequestMapping("preLogin.action")
	public String prelogin() {
		this.front();
		return "users/login";
	}

	// 用户登录
	@RequestMapping("login.action")
	public String login() {
		this.front();
		String username = this.getRequest().getParameter("username");
		String password = this.getRequest().getParameter("password");
		String checkcode = this.getRequest().getParameter("checkcode").toUpperCase();
		String RANDOMVALIDATECODEKEY = (String) this.getSession().getAttribute("RANDOMVALIDATECODEKEY");
		if (checkcode.equals(RANDOMVALIDATECODEKEY)) {
			Users u = new Users();
			u.setUsername(username);
			List<Users> usersList = this.usersService.getUsersByCond(u);
			if (usersList.size() == 0) {
				this.getSession().setAttribute("message", "用户名不存在");
				return "redirect:/index/preLogin.action";
			} else {
				Users users = usersList.get(0);
				if ("未认证".equals(users.getStatus())) {
					this.getSession().setAttribute("message", "账户被锁定");
					return "redirect:/index/preLogin.action";
				}
				if (password.equals(users.getPassword())) {
					this.getSession().setAttribute("userid", users.getUsersid());
					this.getSession().setAttribute("username", users.getUsername());
					this.getSession().setAttribute("users", users);
					return "redirect:/index/index.action";
				} else {
					this.getSession().setAttribute("message", "密码错误");
					return "redirect:/index/preLogin.action";
				}
			}
		} else {
			this.getSession().setAttribute("message", "验证码错误");
			return "redirect:/index/preLogin.action";
		}
	}

	// 准备注册
	@RequestMapping("preReg.action")
	public String preReg() {
		this.front();
		return "users/register";
	}

	// 用户注册
	@RequestMapping("register.action")
	public String register(Users users) {
		this.front();
		Users u = new Users();
		u.setUsername(users.getUsername());
		List<Users> usersList = this.usersService.getUsersByCond(u);
		if (usersList.size() == 0) {
			users.setStatus("未认证");
			users.setRegdate(VeDate.getStringDateShort());
			this.usersService.insertUsers(users);
		} else {
			this.getSession().setAttribute("message", "用户名已存在");
			return "redirect:/index/preReg.action";
		}

		return "redirect:/index/preLogin.action";
	}


	@RequestMapping("usercenter.action")
	public String usercenter() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		return "users/usercenter";
	}

	@RequestMapping("userinfo.action")
	public String userinfo() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		this.getSession().setAttribute("users", this.usersService.getUsersById(userid));
		return "users/userinfo";
	}

	@RequestMapping("personal.action")
	public String personal(Users users) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		this.usersService.updateUsers(users);
		return "redirect:/index/userinfo.action";
	}

	// 留言板
	@RequestMapping("bbs.action")
	public String bbs() {
		this.front();
		List<Bbs> bbsList = this.bbsService.getAllBbs();
		this.getRequest().setAttribute("bbsList", bbsList);
		return "users/bbs";
	}


	// 查看留言
	@RequestMapping("readbbs.action")
	public String readbbs() {
		this.front();
		Bbs bbs = this.bbsService.getBbsById(getRequest().getParameter("id"));
		bbs.setHits("" + (Integer.parseInt(bbs.getHits()) + 1));
		this.bbsService.updateBbs(bbs);
		this.getRequest().setAttribute("bbs", bbs);
		Rebbs rebbs = new Rebbs();
		rebbs.setBbsid(bbs.getBbsid());
		List<Rebbs> rebbsList = this.rebbsService.getRebbsByCond(rebbs);
		this.getRequest().setAttribute("rebbsList", rebbsList);
		return "users/readbbs";
	}

	// 回复留言
	@RequestMapping("rebbs.action")
	public String rebbs() {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Rebbs rebbs = new Rebbs();
		rebbs.setAddtime(VeDate.getStringDate());
		rebbs.setContents(getRequest().getParameter("contents"));
		rebbs.setBbsid(getRequest().getParameter("bbsid"));
		rebbs.setUsersid(userid);
		this.rebbsService.insertRebbs(rebbs);
		Bbs bbs = this.bbsService.getBbsById(rebbs.getBbsid());
		bbs.setRepnum("" + (Integer.parseInt(bbs.getRepnum()) + 1));
		this.bbsService.updateBbs(bbs);
		String path = "redirect:/index/readbbs.action?id=" + bbs.getBbsid();
		return path;
	}


	@RequestMapping("myComplains.action")
	public String myComplains(String number) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Complains complains = new Complains();
		complains.setUsersid(userid);
		List<Complains> complainsList = this.complainsService.getComplainsByCond(complains);
		PageHelper.getIndexPage(complainsList, "complains", "myComplains", null, 10, number, this.getRequest());
		return "users/myComplains";
	}

	@RequestMapping("myClazz.action")
	public String myClazz(String number) {
		this.front();
		if (this.getSession().getAttribute("userid") == null) {
			return "redirect:/index/preLogin.action";
		}
		String userid = (String) this.getSession().getAttribute("userid");
		Clazz clazz = new Clazz();
		clazz.setUsersid(userid);
		List<Clazz> clazzList = this.clazzService.getClazzByCond(clazz);
		PageHelper.getIndexPage(clazzList, "clazz", "myClazz", null, 10, number, this.getRequest());
		return "users/myClazz";
	}

	// 按分类查询
	@RequestMapping("cate.action")
	public String cate(String id, String number) {
		this.front();
		Rooms rooms = new Rooms();
		rooms.setCateid(id);
		List<Rooms> tempList = this.roomsService.getRoomsByCond(rooms);
		PageHelper.getIndexPage(tempList, "rooms", "cate", id, 12, number, this.getRequest());
		return "users/list";
	}

	// 按分类查询
	@RequestMapping("all.action")
	public String all(String number) {
		this.front();
		List<Rooms> tempList = this.roomsService.getAllRooms();
		PageHelper.getIndexPage(tempList, "rooms", "all", null, 12, number, this.getRequest());
		return "users/list";
	}

	@RequestMapping("query.action")
	public String query(String keywords) {
		this.front();
		Rooms rooms = new Rooms();
		rooms.setRoomsname(keywords);
		List<Rooms> roomsList = this.roomsService.getRoomsByLike(rooms);
		this.getRequest().setAttribute("roomsList", roomsList);
		return "users/list";
	}

	// 物资详情
	@RequestMapping("detail.action")
	public String detail(String id) {
		this.front();
		Rooms rooms = this.roomsService.getRoomsById(id);
		this.getRequest().setAttribute("rooms", rooms);
		return "users/detail";
	}

}
